//
//  AYBaseViewController.swift
//  MyWeibo
//
//  Created by 昌俊 杨 on 29/9/2016.
//  Copyright © 2016 April. All rights reserved.
//

import UIKit

class AYBaseViewController: UIViewController {

    var isLogin:Bool = true
    var isPullUp:Bool = false
    
    var tableView: UITableView?
     /** tableView的refresh */
    var tbRefresh:UIRefreshControl = UIRefreshControl()
    /** 添加自定导航栏 */
    lazy var naviBar:UINavigationBar = {()->UINavigationBar in
        let bar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
        return bar
    }()
    lazy var naviItem:UINavigationItem = { () -> UINavigationItem in
        let item = UINavigationItem()
        return item
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        automaticallyAdjustsScrollViewInsets = false
        loadData()
    }
    
    func loadData(){
        tbRefresh.endRefreshing()
    }
}

// MARK: - 设置界面
extension AYBaseViewController
{
    override var title: String?
    {
        didSet{
            naviItem.title = title
    
        }
    }
    
    func setupUI()
    {
        view.backgroundColor = UIColor.cz_random()
        setupNavigationBar()
        isLogin ? setupTableView() : setupVisitorUI()
        
    }
    
    func setupVisitorUI()
    {
        let visitorView = UIView(frame: view.bounds)
        visitorView.backgroundColor = UIColor.white
        view.insertSubview(visitorView, at: 0)
    }
    
    func setupNavigationBar() -> () {
        view.addSubview(naviBar)
         /** 去掉push时的高亮 */
        naviBar.isTranslucent = false
        naviBar.barTintColor = #colorLiteral(red: 0.962233763, green: 0.962233763, blue: 0.962233763, alpha: 1)
        naviBar.items = [naviItem]
        naviBar.titleTextAttributes = [NSForegroundColorAttributeName:#colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1)]
    }
    func setupTableView()
    {
        tableView = UITableView(frame: view.bounds, style: .plain)
        tableView?.delegate = self
        tableView?.dataSource = self
        view.insertSubview(tableView!, belowSubview: naviBar)
        tableView?.contentInset = UIEdgeInsetsMake(naviBar.bounds.height, 0, (tabBarController?.tabBar.bounds.height)!, 0)
        tbRefresh = UIRefreshControl()
        tableView?.addSubview(tbRefresh)
        tbRefresh.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }

}

// MARK: - tableView
extension AYBaseViewController:UITableViewDelegate,UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
     /** 监听上拉刷新 */
//    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//        
//        let row = indexPath.row
//        let section = tableView.numberOfSections - 1
//        
//        if row < 0 || section < 0
//        {
//            return
//        }
//        
//        let count = tableView.numberOfRows(inSection: section)
//        
//        if row == (count-1) && !isPullUp
//        {
//            print("上拉刷新")
//            
//            isPullUp = true
//            
//            loadData()
//        }
//    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let viewContent = view.bounds.height
        let tableContent = scrollView.contentSize.height
        let yOffset = scrollView.contentOffset.y
        if ((tableContent>0) && (tableContent - viewContent < yOffset-100) && !isPullUp)
        {
            print("上拉刷新")
            
            isPullUp = true
            
            loadData()
        }
        
    }
}
